package com.fakeofarty;

import java.util.ArrayList;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.BatteryManager;
import android.os.Binder;
import android.os.IBinder;

public class FtripService extends Service {

	private final IBinder mBinder = new LocalBinder();
	ArrayList<Point> mPointArray = new ArrayList<Point>();

	LocationService mService;

	long mStartTime = 0;
	boolean mStarted = false;
	boolean mBound = false;
	int mBatteryLevel = 0;
	boolean mBack = false;
	float mTotalBackDistance = 0;
	float mBackPoint = 0;

	BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			mBatteryLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
		}
	};

	public class LocalBinder extends Binder {
		FtripService getService() {
			return FtripService.this;
		}
	}

	private ServiceConnection mConnection = new ServiceConnection() {

		@Override
		public void onServiceConnected(ComponentName className, IBinder service) {
			mService = ((LocationService.LocalBinder) service).getService();
			mBound = true;
		}

		@Override
		public void onServiceDisconnected(ComponentName arg0) {
			mBound = false;
		}
	};

	@Override
	public void onCreate() {
		Intent intent = new Intent(this, LocationService.class);
		bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
		registerReceiver(mBatteryReceiver, new IntentFilter(
				Intent.ACTION_BATTERY_CHANGED));
		mStartTime = System.currentTimeMillis();
	};

	@Override
	public void onDestroy() {
		unregisterReceiver(mBatteryReceiver);
		if (mBound) {
			unbindService(mConnection);
			mBound = false;
		}
	};

	@Override
	public IBinder onBind(Intent intent) {
		return mBinder;
	}

	public DistanceData getData() {
		Point lastPoint = mService.getLastPoint(WayPoint.TYPE_USUAL,
				mPointArray.size());
		lastPoint.mDistance -= mTotalBackDistance;
		float totalDistance = lastPoint.mDistance;
		if (!mBack) {
			mBackPoint = lastPoint.mDistance;
		} else {
			totalDistance = mBackPoint * 2 - totalDistance;
		}
		return new DistanceData(System.currentTimeMillis() - mStartTime,
				lastPoint.getDistanceTo(mPointArray, mBackPoint),
				totalDistance, 0, mPointArray.size(), lastPoint.mSpeed,
				mService.getGpsStatus(), 0, mBatteryLevel);
	}

	public void start() {
		mStartTime = System.currentTimeMillis();
		mStarted = true;
		mPointArray = new ArrayList<Point>();
		mService.start();
		mPointArray.add(new Point(0, 0, 0));
		mTotalBackDistance = 0;
		mBackPoint = 0;
		mBack = false;
	}

	public void stop() {
		mStarted = false;
		mService.stop();
	}

	public boolean putPoint() {
		if (!mStarted) {
			start();
			return true;
		} else {
			Point lastPoint = mService.getLastPoint(WayPoint.TYPE_CHECK,
					mPointArray.size());
			if (!mBack) {
				lastPoint.mDistance -= mTotalBackDistance;
				mPointArray.add(lastPoint);
			} else {
				mBackPoint = mPointArray.get(mPointArray.size() - 1).mDistance;
				mTotalBackDistance = lastPoint.mDistance - mBackPoint;
				mPointArray.remove(mPointArray.size() - 1);
			}
		}
		return false;
	};

	public boolean back() {
		Point lastPoint = mService.getLastPoint(WayPoint.TYPE_BACK,
				mPointArray.size());
		lastPoint.mDistance -= mTotalBackDistance;
		if (mBack) {
			mTotalBackDistance += 2 * (lastPoint.mDistance - mBackPoint);
			lastPoint.mDistance -= 2 * (lastPoint.mDistance - mBackPoint);
			mBackPoint = lastPoint.mDistance;
		} else {
			mBackPoint = lastPoint.mDistance;
		}
		mBack = !mBack;
		return mBack;
	}

}